package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.model.*;
import com.rongke.service.EvaluateService;
import com.rongke.service.GoodsService;
import com.rongke.service.MuserService;
import com.rongke.service.OrdersService;
import com.rongke.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/evaluate")
@Transactional
@CrossOrigin(origins = "*",maxAge = 3600)
public class EvaluateController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private MuserService muserService;
    @Autowired
    private GoodsService goodsService;

    /**
     * @param evaluate
     * @return 返回值JsonResp
     * @添加商品评价
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addEvaluate(@RequestBody Evaluate evaluate) {
        log.debug("添加商品评价");
        if (null == evaluate.getGoodsId() || null == evaluate.getMuserId())
            return JsonResp.toFail("server:商品id和用户id不能为空");
        evaluate.setGmtDatetime(new Date());
        evaluateService.insert(evaluate);
        return JsonResp.ok(evaluate);
    }

    /**
     * @param comments
     * @return 返回值JsonResp
     * @添加商品评价
     */
    @RequestMapping(value = "/addBatch", method = RequestMethod.POST)
    public JsonResp addEvaluateBatch(@RequestBody List<Evaluate> comments, Long orderId) {
        log.debug("添加商品评价");
        for (Evaluate evaluate : comments) {
            if (null == evaluate.getGoodsId() || null == evaluate.getMuserId())
                return JsonResp.toFail("server:商品id和用户id不能为空");
            evaluate.setGmtDatetime(new Date());
        }
        evaluateService.insertBatch(comments);
        Orders orders = new Orders();
        orders.setId(orderId);
        //设置订单状态为 4 已评价
        orders.setUptDatetime(new Date());
        orders.setCommentTime(new Date());
        orders.setOrderStatus(4);
        ordersService.updateById(orders);
        return JsonResp.ok(comments);
    }

    /**
     * @param evaluate
     * @return 返回值JsonResp
     * @修改商品评价
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateEvaluate(@RequestBody Evaluate evaluate) {
        log.debug("修改商品评价");
        evaluateService.updateById(evaluate);
        return JsonResp.ok(evaluate);
    }

    /**
     * @param id
     * @return 返回值JsonResp
     * @根据id查找商品评价
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectEvaluate(Long id) {
        log.debug("查找商品评价");
        Evaluate evaluate = evaluateService.selectById(id);
        return JsonResp.ok(evaluate);
    }

    /**
     * 删除评论
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteComment", method = RequestMethod.GET)
    public JsonResp deleteComment(Long id) {
        log.debug("删除评论");
        evaluateService.deleteById(id);
        return JsonResp.ok("删除评论成功");
    }

    /**
     * @param goodsId
     * @return
     * @根据商品id获取详情评价
     */
    @RequestMapping(value = "/selectEvaluateListByGoodsId", method = RequestMethod.GET)
    public JsonResp selectEvaluateListByGoodsId(Long goodsId, Page page) {
        log.debug("根据商品id获取详情评价");
        if (null == goodsId)
            return JsonResp.toFail("goodsId参数不能为空");
        EntityWrapper<Evaluate> ew = new EntityWrapper<>();
        ew.eq("goods_id", goodsId);
//        Page<Evaluate> evaluatePage =evaluateService.selectPage(page,ew);
        Map map = new HashMap();
        Integer startIndex = page.getSize() * (page.getCurrent() - 1);
        Integer size = page.getSize();
        map.put("goodsId", goodsId);
        map.put("startIndex", startIndex);
        map.put("size", size);
        List<CommentResponse> commentList = evaluateService.selectLimitedCommentsByGoodsId(map);
        for (CommentResponse response : commentList) {
            //1表示匿名
            if (null != response.getIsAnonymous() && response.getIsAnonymous() == 1) {
                if (response.getUserName().length() > 1) {
                    String origName = response.getUserName();
                    StringBuffer nameSB = new StringBuffer(origName);
                    nameSB.replace(1, 2, "*");
                    response.setUserName(nameSB.toString());
                }
            }
        }
        Page<CommentResponse> commentPage = new Page<>();
        commentPage.setRecords(commentList);
        return JsonResp.ok(commentPage);
    }


    /**
     * 后台获取所有评论
     * @param page
     * @return
     */
    @RequestMapping(value = "/getCommentsAdmin", method = RequestMethod.GET)
    public JsonResp getCommentsAdmin(Page page) {
        log.debug("后台获取所有评论");
        EntityWrapper<Evaluate> ew = new EntityWrapper<>();
        Map map = page.getCondition();
        Object obj=map.get("searchText");
        String search = map.get("searchText").toString();
        page.setCondition(null);
        if (!search.isEmpty()) {
            ew.like("content", search);
        }
        ew.orderBy("gmt_datetime", false);
        page = evaluateService.selectPage(page, ew);
        List<Evaluate> evaluates=page.getRecords();
        evaluates.forEach(e->{
            Muser muser=muserService.selectById(e.getMuserId());
            Goods goods=goodsService.selectById(e.getGoodsId());
            e.setGoods(goods);
            e.setMuser(muser);
        });
        return JsonResp.ok(page);
    }

}
